#include "messageeditarea.h"
#include "mainwidget.h"
#include "historymessagewidget.h"

MessageEditArea::MessageEditArea(QWidget *parent)
    : QWidget{parent}
{
    // 1. 设置必要的属性
    this->setFixedHeight(200);
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    this->setStyleSheet("border-width: 1px 0 0 0; border-style: solid; border-color: rgb(215, 215, 215)");

    // 2. 创建垂直方向的布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0,0,10,10);
    this->setLayout(vlayout);

    // 3. 创建水平方向的布局管理器
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(10);
    hlayout->setContentsMargins(10,10,0,0);
    hlayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    vlayout->addLayout(hlayout);

    // 4. 把上方的四个按钮, 创建好并添加到水平布局中
    // 统一样式
    QString btnStyle = "QPushButton { background-color: rgb(225, 225, 225); border: none; } QPushButton:pressed { background-color: rgb(210, 210, 210); }";
    QSize btnSize(25,25);
    QSize iconSize(25,25);

    sendImageBtn = new QPushButton();
    sendFileBtn = new QPushButton();
    sendSpeechBtn = new QPushButton();
    showHistoryBtn = new QPushButton();

    sendImageBtn->setFixedSize(btnSize);
    sendImageBtn->setIconSize(iconSize);
    sendImageBtn->setIcon(QIcon(":/resource/image/image.png"));
    sendImageBtn->setStyleSheet(btnStyle);

    sendFileBtn->setFixedSize(btnSize);
    sendFileBtn->setIconSize(iconSize);
    sendFileBtn->setIcon(QIcon(":/resource/image/file.png"));
    sendFileBtn->setStyleSheet(btnStyle);

    sendSpeechBtn->setFixedSize(btnSize);
    sendSpeechBtn->setIconSize(iconSize);
    sendSpeechBtn->setIcon(QIcon(":/resource/image/sound.png"));
    sendSpeechBtn->setStyleSheet(btnStyle);

    showHistoryBtn->setFixedSize(btnSize);
    showHistoryBtn->setIconSize(iconSize);
    showHistoryBtn->setIcon(QIcon(":/resource/image/history.png"));
    showHistoryBtn->setStyleSheet(btnStyle);

    hlayout->addWidget(sendImageBtn);
    hlayout->addWidget(sendFileBtn);
    hlayout->addWidget(sendSpeechBtn);
    hlayout->addWidget(showHistoryBtn);

    // 5. 添加多行编辑框
    textEdit = new QPlainTextEdit();
    textEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    textEdit->setStyleSheet("QPlainTextEdit { border: none; background-color: transparent; font-size: 14px; padding: 10px; }");
    QString barStyle = "QScrollBar:vertical {border: none;background: rgb(225,225,225);width: 10px;margin: 0px 0px 0px 0px;}\
        QScrollBar::handle:vertical {background: #c1c1c1;min-height: 20px;border-radius: 5px;}\
        QScrollBar::handle:vertical:hover {background: #a8a8a8;}\
        QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {height: 0px;}\
        QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {background: none;}";
    textEdit->verticalScrollBar()->setStyleSheet(barStyle);
    vlayout->addWidget(textEdit);

    // 6. 添加发送文本消息的按钮
    sendBtn = new QPushButton();
    sendBtn->setText("发送消息");
    sendBtn->setFixedSize(120,40);
    QString style = "QPushButton { font-size: 16px; color: rgb(7, 193, 96); border: none; background-color: rgb(233, 233, 233); border-radius: 10px; } ";
    style += "QPushButton:hover { background-color: rgb(210, 210, 210); }";
    style += "QPushButton:pressed { background-color: rgb(190, 190, 190); }";
    sendBtn->setStyleSheet(style);
    vlayout->addWidget(sendBtn,0,Qt::AlignRight | Qt::AlignVCenter);

    // 7. 初始化信号槽
    initSignalSlot();
}

void MessageEditArea::initSignalSlot()
{
    // 聊天记录
    connect(showHistoryBtn,&QPushButton::clicked,this,[=](){
        MainWidget* main = MainWidget::getInstance();
        HistoryMessageWidget* history = new HistoryMessageWidget(main);
        history->show();
    });
}

void MessageEditArea::paintEvent(QPaintEvent *event)
{
    (void) event;
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

